package com.ruoyi.web.controller.class_choose.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.web.controller.classes.domain.AClasses;
import com.ruoyi.web.controller.classes.service.IAClassesService;
import com.ruoyi.web.controller.pic.domain.APic;
import com.ruoyi.web.controller.pic.service.IAPicService;
import com.ruoyi.web.controller.study_path.domain.ARoute;
import com.ruoyi.web.controller.study_path.service.IARouteService;
import org.apache.poi.ss.formula.functions.Choose;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.controller.class_choose.domain.AChooseClass;
import com.ruoyi.web.controller.class_choose.service.IAChooseClassService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * class_chooseController
 *
 * @author G
 * @date 2025-04-28
 */
@RestController
@RequestMapping("/class/class_choose")
public class AChooseClassController extends BaseController {
  @Autowired
  private IAChooseClassService aChooseClassService;
  @Autowired
  private IAClassesService aClassesService;

  @Autowired
  private IARouteService aRouteService;
  @Autowired
  private IAPicService aPicService;
  /**
   * 查询class_choose列表
   */
  @PreAuthorize("@ss.hasPermi('class:class_choose:list')")
  @GetMapping("/list")
  public TableDataInfo list(AChooseClass aChooseClass) {
    startPage();
    List<AChooseClass> list = aChooseClassService.selectAChooseClassList(aChooseClass);
    return getDataTable(list);
  }

  /**
   * 导出class_choose列表
   */
  @PreAuthorize("@ss.hasPermi('class:class_choose:export')")
  @Log(title = "class_choose", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, AChooseClass aChooseClass) {
    List<AChooseClass> list = aChooseClassService.selectAChooseClassList(aChooseClass);
    ExcelUtil<AChooseClass> util = new ExcelUtil<AChooseClass>(AChooseClass.class);
    util.exportExcel(response, list, "class_choose数据");
  }

  /**
   * 获取class_choose详细信息
   */
  @PreAuthorize("@ss.hasPermi('class:class_choose:query')")
  @GetMapping(value = "/{Id}")
  public AjaxResult getInfo(@PathVariable("Id") Long userId) {
    AChooseClass list = aChooseClassService.selectAChooseClassByUserId(userId);
    System.out.println(list);
    return success(list);
  }

  /**
   * 新增class_choose
   */
  @PreAuthorize("@ss.hasPermi('class:class_choose:add')")
  @Log(title = "class_choose", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@RequestBody AChooseClass aChooseClass) {
    return toAjax(aChooseClassService.insertAChooseClass(aChooseClass));
  }

  /**
   * 修改class_choose
   */
  @PreAuthorize("@ss.hasPermi('class:class_choose:edit')")
  @Log(title = "class_choose", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@RequestBody AChooseClass aChooseClass) {
    return toAjax(aChooseClassService.updateAChooseClass(aChooseClass));
  }

  /**
   * 删除class_choose
   */
  @PreAuthorize("@ss.hasPermi('class:class_choose:remove')")
  @Log(title = "class_choose", businessType = BusinessType.DELETE)
  @DeleteMapping("/{userIds}")
  public AjaxResult remove(@PathVariable Long[] userIds) {
    return toAjax(aChooseClassService.deleteAChooseClassByUserIds(userIds));
  }


//--------------------------------------------------------------------------------------------------------------------------

  /**
   * 查询class_choose列表
   */
  @GetMapping("/list_s")
  public TableDataInfo list_s(AClasses aClasses) {
    startPage();
    List<Map<String, Object>> list = aClassesService.selectAClassesList_t_all();
    return getDataTable(list);
  }

  /**
   * 选课
   */
  @Log(title = "insert", businessType = BusinessType.INSERT)
  @GetMapping("/insert")
  public AjaxResult insert(@RequestParam("classId") Long classId) {
    AChooseClass aChooseClass = new AChooseClass();
    aChooseClass.setClassId(classId);
    aChooseClass.setUserId(getUserId());
    aChooseClass.setClassProgress(0L);
    aChooseClass.setClassScore(0L);
    try {
      // 尝试插入选课记录
      int result = aChooseClassService.insertAChooseClass(aChooseClass);
      if (result > 0) {
        return AjaxResult.success("选课成功");
      } else {
        return AjaxResult.error("选课失败");
      }
    } catch (Exception e) {
      // 捕获异常并返回“已经选课”的提示
      if (e.getMessage() != null && e.getMessage().contains("课程已选")) {
        return AjaxResult.error(400, "已经选课");
      } else {
        return AjaxResult.error("课程已选");
      }
    }
  }

  /**
   * 查询个人class列表
   */
  @GetMapping("/list_myself")
  public TableDataInfo list_myself(AChooseClass aChooseClass) {
    startPage();
    List<HashMap<String, Object>> list = aChooseClassService.selectAChooseClassListS();
    return getDataTable(list);
  }

  /**
   * 退课操作
   */
  @DeleteMapping("/out/{id}")
  public AjaxResult out(@PathVariable("id") Long id) {
    AChooseClass aChooseClass = new AChooseClass();
    aChooseClass.setId(id);
    List<AChooseClass> aChooseClasses = aChooseClassService.selectAChooseClassList(aChooseClass);
    for (int i = 0; i < aChooseClasses.size(); i++) {
      System.out.println("aChooseClasses = " + aChooseClasses.get(i));
    }
    APic apic = new APic();
    apic.setClassId(aChooseClasses.get(0).getClassId());
    apic.setUserId(aChooseClasses.get(0).getUserId());
    ARoute aroute = new ARoute();
    aroute.setClassId(aChooseClasses.get(0).getClassId());
    aroute.setStudentId(aChooseClasses.get(0).getUserId());

    List<APic> aPics = aPicService.selectAPicList(apic);
    List<ARoute> aRoutes = aRouteService.selectARouteList(aroute);
    for (int i = 0; i < aPics.size(); i++) {
      System.out.println("aPics = " + aPics.get(i));
    }
    for (int i = 0; i < aRoutes.size(); i++) {
      System.out.println("aRoutes = " + aRoutes.get(i));
    }
    aPicService.deleteAPicByPicId(aPics.get(0).getPicId());
    aRouteService.deleteARouteByRouteId(aRoutes.get(0).getRouteId());
    return toAjax(aChooseClassService.deleteFromClassId(id));
  }

  /**
   * 查询考题
   */
  @GetMapping("/getExam/{classId}")
  public List<Map<String, Object>> getExam(@PathVariable("classId") Long classId) {
    List<Map<String, Object>> exam = aClassesService.getExam(classId);
    System.out.println("exam = " + exam);
    return exam;
  }

  /**
   * 更新得分
   */
  @GetMapping("/updateScore")
  public AjaxResult updateScore(@RequestParam("classId") Long classId, @RequestParam("score") Long score) {
    AChooseClass aChooseClass = new AChooseClass();
    aChooseClass.setClassId(classId);
    aChooseClass.setUserId(getUserId());
    aChooseClass.setClassScore(score);
    return toAjax(aChooseClassService.updateAChooseClass(aChooseClass));
  }


}
